<script type="text/javascript">
    layui.use(['form', 'layer', 'jquery', 'fly', 'face', 'laypage'], function () {
        $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var fly = layui.fly;
        var laypage = layui.laypage;
        var userId = "{{$data['user']['id']}}";//当前用户id
        var forumId = "{{$data['id']}}";//帖子id
        var token = "{{csrf_token()}}";

        //异步获取评论列表
        $(document).ready(function () {
            var data = {id: forumId, page: 1};
            getLists(data);
        });

        //获取列表
        function getLists(data) {
            $.ajax({
                url: '/forumCommentList',
                type: 'get',
                data: data,
                dataType: "json",
                success: function (result) {
                    if (result.code == 0) {
                        editor(result.data);
                    }
                }
            });
        }

        //重新渲染文本编辑器
        function editor(data) {
            $('#commentList').html(data);
            //如果你是采用模版自带的编辑器，你需要开启以下语句来解析。
            $('#commentList .detail-body').each(function () {
                var othis = $(this), html = othis.html();
                othis.html(fly.content(html));
            });
        }

        //动态分页
        laypage.render({
            elem: 'page'
            , count: "{{$data['get_comment_count']}}"
            , theme: '#2db5a3'
            , limit: 10
            , layout: ['page', 'next']
            , jump: function (obj, first) {
                if (!first) {
                    var data = {id: forumId, page: obj.curr};
                    getLists(data);
                    scroll();
                }
            }
        });

        //公共方法form提交
        form.on('submit(submitIng)', function (data) {
            if(!userId){
                layer.msg('骚年登录后才能评论！', {time: 1000}, function () {
                    $(window).attr('location', '/login');
                });
            }
            var url = $('form').data('url');
            $('button').addClass("layui-btn-disabled");//改成禁止点击样式
            $('button').attr("disabled", 'disabled');//禁止按钮点击

            $.ajax({
                url: url, type: 'post', data: data.field, dataType: "json",
                success: function (result) {
                    if (result.code === 0) {
                        layer.msg(result.msg, {time: 1000, icon: 6}, function () {
                            scroll();
                            editor(result.data);
                            $("textarea").val('');
                            $('button').attr("class", "layui-btn layui-btn-sm");
                            $('button').removeAttr("disabled");
                        });
                    } else {
                        layer.msg(result.msg, {time: 2000, icon: 5}, function () {
                            $('button').attr("class", "layui-btn layui-btn-sm");
                            $('button').removeAttr("disabled");
                        });
                    }
                }
            });
            return false;
        });

        //删除帖子
        $('#delete').on('click', function () {
            var url = $(this).data('url');
            layer.confirm('确定要删除吗？', function () {
                $.ajax({
                    url: url, type: 'delete', data: {'id': forumId, '_token': '{{csrf_token()}}'},
                    success: function (result) {
                        if (result.code === 0) {
                            layer.msg(result.msg, {time: 1000, icon: 6}, function () {
                                $(window).attr('location', '/forumList');
                            });
                        } else {
                            layer.msg(result.msg, {time: 2000, icon: 5});
                        }
                    }
                });

            });
        });

        //评论帖子
        $('#comm').click(function () {
            $("input[name='type']").val(1);
            $("input[name='forum_id']").val(forumId);
        });

        //回复帖子
        $(document).on('click', '.reply', function () {
            $("input[name='type']").val(2);
            $("input[name='p_id']").val($(this).data('pid'));
            var that = $(this).parent().parent().find('cite');
            var authName = '@' + that.html() + ' ';
            $("textarea").val(authName);
        });

        //评论点赞
        $(document).on('click', '.commentZan', function () {
            var that = $(this);
            var zan = that.attr('type');
            var num = that.find('em');
            var commentId = that.data('id');//评论id
            var data = {forum_id: forumId, comment_id: commentId, type: 1, zan: zan, _token: token};
            myOperate(data).then(function (result) {
                if (result.code == 0) {
                    if (zan == 'zanno') {
                        $(num).html(Number(num.html()) + 1);
                        that.attr('type', 'zanok');
                        that.css('color', '#c00');
                    }
                    if (zan == 'zanok') {
                        $(num).html(Number(num.html()) - 1);
                        that.attr('type', 'zanno');
                        that.css('color', '#999');
                    }
                } else {
                    layer.msg(result.msg);
                }
            });
        });

        //评论删除
        $(document).on('click', '.commentDel', function () {
            var that = $(this);
            var commentId = that.data('id');//评论id
            layer.open({
                content: '确认删除此评论吗？'
                , btn: ['确认', '取消']
                , yes: function (index) {
                    layer.close(index);
                    var data = {id: commentId, type: '3', _token: token};
                    myOperate(data).then(function (result) {
                        if (result.code == 0) {
                            that.parent().parent().parent().remove();
                            $('#comTotal').html(Number($('#comTotal').html()) - 1);
                        }
                        layer.msg(result.msg);
                    });
                }
            });
        });

        //评论是否采纳
        $(document).on('click', '.commentAccept', function () {
            var that = $(this);
            var commentId = that.data('id');//评论id
            var accept = that.attr('type');
            layer.open({
                content: '确认' + that.html() + '此评论吗？'
                , btn: ['确认', '取消']
                , yes: function (index) {
                    layer.close(index);
                    var data = {forum_id: forumId, comment_id: commentId, type: 2, accept: accept, _token: token};
                    myOperate(data).then(function (result) {
                        if (result.code == 0) {
                            editor(result.data);
                            scroll();
                            layer.msg("设置成功！");
                        } else {
                            layer.msg("设置失败！");
                        }
                    });
                }
            });
        });

        /**
         * 操作方法
         * data.type(1点赞，2采纳，3删除评论)
         */
        function myOperate(data) {
            if (data) {
                return $.ajax({
                    url: '/forumOperate', type: 'post', data: data, dataType: "json"
                }).then(function (data) {
                    return data;
                });
            } else {
                layer.msg('参数错误');
            }
        }

        //动画滚动
        function scroll() {
            var scroll_offset = $('#flyReply').offset();
            $("body,html").animate({
                scrollTop: scroll_offset.top
            }, 100);
        }
    });

</script>
